import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import AboutView from '../views/AboutView.vue'
import StudentView from '../views/StudentView.vue'
import CampusBinhai from '../views/CampusBinhai.vue'
import CampusChashan from '../views/CampusChashan.vue'
import CampusYueqing from '../views/CampusYueqing.vue'
import Login from '../views/Login.vue'
import StudentSystem from '../views/StudentSystem.vue'



Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component:AboutView
  },
  {
    path: '/student',
    name: 'student',
    component: StudentView
  },
  {
    path: '/CampusBinhai',
    name: 'CampusBinhai',
    component: CampusBinhai
  },
  {
    path: '/campus-chashan',
    name: 'CampusChashan',
    component: CampusChashan
  },
  {
    path: '/campus-yueqing',
    name: 'CampusYueqing',
    component: CampusYueqing
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/student-system',
    name: 'StudentSystem',
    component: StudentSystem,
    meta: { requiresAuth: true },
    beforeEnter:(to,from,next)=>{
      next('/login');
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!localStorage.getItem('token')) {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
